var marqueeObject, WC_CD = '100301',
	maxAjax = 1000,
	nowAjax = 0;
$(function() {
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			this.getTopLeftChart(); //每日完工数量
			this.getTopRightChart(); //每日废品数量
			this.getTopTableData(); //KPI表格数据
			// this.getBottomChart();
		},
		getTopTableData(falg) {
			iplantAjaxRequest({
				url: '/iPlant_ajax',
				data: {
					IFS: 'MES_REP111',
					WC_CD: WC_CD
				},
				successCallBack: function(data) {
					console.log('data1111', data);
					var count = 0;
					if (data.RESPONSE[0].length != 0) {
						var rowCollection = createSourceObj(data);
						var html = "";
						// var plan = 0, //计划总和
						// 	output = 0, //产出总和
						// 	tou = 0; //投入总和
						html += '<table id="tablehuizong" class="t-new-style">'
						if (rowCollection.length > 0) {
							for (var i = 0; i < rowCollection.length; i++) {
								// plan += parseInt(rowCollection[i].PLAN_WO_QTY);//计划产量
								// tou += parseInt(rowCollection[i].PRO_QTY);// 投入
				
								// WO_STATE =7 待  = 2 生产中
								html += "<tr height='25px'><td>" + (rowCollection[i].PL_NM || "-") + "</td>" + //拉线编码 
									"<td>" + rowCollection[i].THISMO + "</td>" + //工单号
									"<td>" + rowCollection[i].SO_NO + "</td>" + //销售订单
									"<td>" + rowCollection[i].ITEM_NM + "</td>" + //物料名称
									"<td>" + rowCollection[i].MO_MODEL_NM + "</td>" + //助记码
									"<td>" + rowCollection[i].LEIJI_PLAN_QTY + "</td>" + //累计排产
									"<td>" + rowCollection[i].PRO_QTY + "</td>"+ //投入
									"<td>" + rowCollection[i].HEGESHULIAN + "</td>";//产出
				
								// 7待检验，2已审核，5生产中，4结案，3暂停，1，新建、8/抛单
								// html += "<td>" + rowCollection[i].HEGESHULIAN + "</td>"; //产出
								// output += parseInt(rowCollection[i].HEGESHULIAN)
								var stateText = (function(state) {
									var str = '-'
									switch (state) {
										case "0":
											str = '已投入';
											break;
										case "1":
											str = '待检验';
											break;
										case "2":
											str = '已完成';
											break;
										default:
											str = '-'
									}
									return str;
				
								})(rowCollection[i].WO_STATE);
								console.log("stateText====>",stateText);
								var buNew = '';
								var bu = rowCollection[i].BULIANSHU;
								// console.log('BULIANSHU1111=====>',rowCollection[i].BULIANSHU);
								if(bu == null){
									 buNew = '0'
								}else{
									buNew = rowCollection[i].BULIANSHU
								}
								// console.log('BULIANSHU22222=====>',buNew);
								html += "<td>" + buNew + "</td>" + //不良数
									"<td>" + stateText + "</td></tr>"; //产线生产状态
							}
							html += '</table>';
							
							//处理滚动
							var hei = $('.marqueeBody').height() - 32;
							var mlen = Math.floor(hei / 25);
							$("#huizong").html('').height(hei).append(html);
				
							if (marqueeObject) {
								marqueeObject.liMarquee('destroy');
							}
							if (rowCollection.length <= mlen) {
								marqueeObject = $("#huizong").liMarquee({
									direction: 'up',
									scrollamount: 20,
									runshort: false
								})
							} else {
								marqueeObject = $("#huizong").liMarquee({
									direction: 'up',
									scrollamount: 20
								})
							}
							// planOut(plan, output, tou);//计划产出
							// workings(output/plan*100, output/tou*100);//生产进度
						}
					}
				}
			});
		},
		getBottomChart() {
			//1,先准备数据
			var datas = [{
				contractNumber: "甲-1047002",
				mnemonicCode: "SlR-226 13-8 35E",
				score: "扣题-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "3047105",
				mnemonicCode: "javascript",
				score: "扣题-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "3047105",
				mnemonicCode: "javascript",
				score: "扣题-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "3047105",
				mnemonicCode: "javas12cript",
				score: "扣题-机加工123吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "3047105111",
				mnemonicCode: "javascript",
				score: "扣题-机加工吧213213",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "3047106d33",
				mnemonicCode: "SLR-1009-8-10 23A",
				score: "环体-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "WWW3047106d33",
				mnemonicCode: "SLR-1009-8-10 23A",
				score: "环体-机加工吧12d",
				contractQuantity: "322",
				plannedQuantity: "8211",
				dispatchingQuantity: "72082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "304354eeq33",
				mnemonicCode: "SLR-1009-8-10 23A",
				score: "ewrwer环体-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}, {
				contractNumber: "wwwww106d33",
				mnemonicCode: "SLR-1009-8-10 23A",
				score: "ghghh环体-机加工吧",
				contractQuantity: "8000",
				plannedQuantity: "8211",
				dispatchingQuantity: "7082",
				baogongQuantity: "290",
				processingTime: "25241",
				perprocessingTime: "87.04",
				banchanHour: "41.1",
				deviceName: "数控羊角钻孔专机06",
				baogongUser: "张三",
				baogongTime: "2021-11-01 10:05",
				rukuTime: "2021-11-01 10:42"
			}];

			//2,往tbody里面创建行，有几个人（通过数组的长度）我们就创建几行
			var tbody = document.querySelector("tbody");
			for (var i = 0; i < datas.length; i++) //外面的for循环 是 行tr
			{
				var tr = document.createElement("tr");
				tbody.appendChild(tr);
				//3,往tr每一行里面创建单元格（跟数据有关系的3个单元格），td单元格的数量取决于每个对象的属性个数 for循环遍历对象 datas[i]
				for (var k in datas[i]) //里面的for循环是 列
				{
					var td = document.createElement("td"); //创建单元格
					tr.appendChild(td);
					td.innerHTML = datas[i][k]; //把对象里面的属性值 datas[i][k]给td
				}

			}

			//table 内容滚动 开始
			function change() {
				var body = $("#tbody");
				body.append(body.children().first());
			}
			$(document).ready(function() {
				$("#demo").mouseenter(function() {
					clearInterval(MyMar1)
				});
				$("#demo").mouseleave(function() {
					MyMar1 = setInterval(change, 5000);
				});
			});
			var MyMar1 = setInterval(change, 5000);
			//table 内容滚动 end

		},

		getTopLeftChart() {
			let datalocalleft = {
				chart: {
					type: 'bar'
				},
				title: {
					text: '每日完工数量'
				},
				credits: {
					enabled: false //?禁用版权信息
				},
				exporting: { // 右上角导出按钮关闭
					buttons: {
						contextButton: {
							enabled: false
						}
					}
				},
				xAxis: {
					categories: ['朱光慧', '谢鑫鹏', '欧阳滕', '朱炜俊'],
					title: {
						text: null
					}
				},
				yAxis: {
					min: 0,
					title: {
						text: '完工总量 (个)',
						align: 'high'
					},
					labels: {
						overflow: 'justify'
					}
				},
				tooltip: {
					valueSuffix: ' 个'
				},
				plotOptions: {
					bar: {
						dataLabels: {
							enabled: true,
							allowOverlap: true, // 允许数据标签重叠
							style: {
								"color": "contrast",
								"fontSize": "16px",
								"fontWeight": "bold",
								"textOutline": "1px 1px contrast"
							}
						}
					},
					series: {
						borderWidth: 0
					}
				},
				legend: {
					backgroundColor: ((Highcharts.theme && Highcharts.theme
						.legendBackgroundColor) || '#FFFFFF'),
					shadow: true
				},
				series: [{
					name: '每日完工数量',
					data: [107, 301, 203, 200]
				}]
			}
			iplantAjaxRequest({
				url: '/iPlant_ajax',
				data: {
					IFS: 'MES_REP771', //测试接口
				},
				successCallBack: function(data) {
					// 这里处理接口返回数据再赋值即可
					console.log('getTopLeftChart', data);
					if (data.RESPONSE[0].RESPONSE_DATA.length > 0) {
						var mockData = {
							"RESPONSE": [{
								"RESPONSE_HDR": {
									"MSG_CODE": "0",
									"MSG_TEXT": "查询成功",
									"MSG_LEVEL": "",
									"RESULT_NUM": "2",
									"MSG_ID": "",
									"PACKAGE_FLAG": "0",
									"DATA_ROWS": "11",
									"RUN_TIMES": "38",
									"MSG_TRACE": ""
								},
								"RESPONSE_DATA": [{
									"NAMES": "刘成龙",
									"QTY": "522",
									"DATES": "2021-11-10"
								}, {
									"NAMES": "刘成龙",
									"QTY": "512",
									"DATES": "2021-11-10"
								}, {
									"NAMES": "刘成龙",
									"QTY": "502",
									"DATES": "2021-11-10"
								}, {
									"NAMES": "刘成龙",
									"QTY": "582",
									"DATES": "2021-11-10"
								}]
							}]
						}
						// let datas = mockData.RESPONSE[0].RESPONSE_DATA;
						let datas = data.RESPONSE[0].RESPONSE_DATA;
						let NAMES = datas.map(function(item) {
							return item.NAMES
						});
						let QTY = datas.map(function(item) {
							return Number(item.QTY)
						});

						//xAxis
						datalocalleft.xAxis.categories = [];
						datalocalleft.xAxis.categories = NAMES;

						//每日完工数量
						datalocalleft.series[0].data = [];
						datalocalleft.series[0].data = QTY;
						var chart = Highcharts.chart('container1', datalocalleft);
					} else {
						var responseTxt = "暂无数据 ， MSG_TEXT:" + data.RESPONSE[0].RESPONSE_HDR
							.MSG_TEXT + "MSG_CODE:" + data.RESPONSE[0].RESPONSE_HDR.MSG_CODE;
						$("#container1").html(responseTxt);
						$("#container1").addClass("fontcss");
					}
				}
			})

		},
		getTopRightChart() {
			let datalocalright = {
				chart: {
					type: 'bar'
				},
				title: {
					text: '每日废品数量'
				},
				credits: {
					enabled: false //?禁用版权信息
				},
				exporting: { // 右上角导出按钮关闭
					buttons: {
						contextButton: {
							enabled: false
						}
					}
				},
				xAxis: {
					categories: ['朱光慧', '谢鑫鹏', '欧阳滕', '朱炜俊'],
					title: {
						text: null
					}
				},
				yAxis: {
					min: 0,
					title: {
						text: '废品数量 (个)',
						align: 'high'
					},
					labels: {
						overflow: 'justify'
					}
				},
				tooltip: {
					valueSuffix: ' 个'
				},
				plotOptions: {
					bar: {
						dataLabels: {
							enabled: true,
							allowOverlap: true, // 允许数据标签重叠
							style: {
								"color": "contrast",
								"fontSize": "16px",
								"fontWeight": "bold",
								"textOutline": "1px 1px contrast"
							}
						}
					},
					series: {
						borderWidth: 0
					}
				},
				legend: {
					backgroundColor: ((Highcharts.theme && Highcharts.theme
						.legendBackgroundColor) || '#FFFFFF'),
					shadow: true
				},
				series: [{
					name: '每日废品数量',
					data: [2, 3, 0, 1],
					color: 'rgba(255,0,0,.9)'
				}]
			}
			iplantAjaxRequest({
				url: '/iPlant_ajax',
				data: {
					IFS: 'MES_REP776',
				},
				successCallBack: function(data) {
					// 这里处理接口返回数据再赋值即可
					console.log('getTopRightChart', data);
					if (data.RESPONSE[0].RESPONSE_DATA.length > 0) {
						var mockData = {
							"RESPONSE": [{
								"RESPONSE_HDR": {
									"MSG_CODE": "0",
									"MSG_TEXT": "查询成功",
									"MSG_LEVEL": "",
									"RESULT_NUM": "2",
									"MSG_ID": "",
									"PACKAGE_FLAG": "0",
									"DATA_ROWS": "11",
									"RUN_TIMES": "38",
									"MSG_TRACE": ""
								},
								"RESPONSE_DATA": [{
									"DATAS": "2021-05-25",
									"BULIANGLV": "0",
									"USERNAME": "刘成龙"
								},{
									"DATAS": "2021-05-25",
									"BULIANGLV": "1",
									"USERNAME": "戈文潮"
								},{
									"DATAS": "2021-05-25",
									"BULIANGLV": "0",
									"USERNAME": "刘成龙"
								},{
									"DATAS": "2021-05-25",
									"BULIANGLV": "1",
									"USERNAME": "戈文潮"
								}]
							}]
						}
						// let datas = mockData.RESPONSE[0].RESPONSE_DATA;
						let datas = data.RESPONSE[0].RESPONSE_DATA;
						let USERNAME = datas.map(function(item) {
							return item.USERNAME
						});
						let BULIANGLV = datas.map(function(item) {
							return Number(item.BULIANGLV)
						});

						//xAxis
						datalocalright.xAxis.categories = [];
						datalocalright.xAxis.categories = USERNAME;

						//每日完工数量
						datalocalright.series[0].data = [];
						datalocalright.series[0].data = BULIANGLV;

						var chart = Highcharts.chart('container2', datalocalright);
					} else {
						var responseTxt = "暂无数据 ， MSG_TEXT:" + data.RESPONSE[0].RESPONSE_HDR
							.MSG_TEXT + "MSG_CODE:" + data.RESPONSE[0].RESPONSE_HDR.MSG_CODE;
						$("#container2").html(responseTxt);
						$("#container2").addClass("fontcss");
					}

				}
			})

		},

	}
	index.init();
	setInterval(function() {
		if (nowAjax >= maxAjax) {
			location.reload();
		} else {
			index.init();
			++nowAjax;
		}
	}, 20 * 1000)
})
